package cn.iocoder.yudao.module.fengdong.smartelderly.controller.app.oldman;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.oldman.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.oldman.OldmanConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.oldman.OldmanDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.oldman.OldmanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "用户 APP - 老人管理")
@RestController
@RequestMapping("/fengdong.smartelderly/oldman")
@Validated
public class AppOldmanController {

    @Resource
    private OldmanService oldmanService;

    @PostMapping("/create")
    @Operation(summary = "创建老人管理")
    public CommonResult<Long> createOldman(@Valid @RequestBody OldmanCreateReqVO createReqVO) throws IOException {
        return success(oldmanService.createOldman(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新老人管理")
    public CommonResult<Boolean> updateOldman(@Valid @RequestBody OldmanUpdateReqVO updateReqVO) throws IOException {
        oldmanService.updateOldman(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除老人管理")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteOldman(@RequestParam("id") Long id) {
        oldmanService.deleteOldman(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得老人管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<OldmanRespVO> getOldman(@RequestParam("id") Long id) {
        OldmanDO oldman = oldmanService.getOldman(id);
        return success(OldmanConvert.INSTANCE.convert(oldman));
    }

    @GetMapping("/list")
    @Operation(summary = "获得老人管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<OldmanRespVO>> getOldmanList(@RequestParam("ids") Collection<Long> ids) {
        List<OldmanDO> list = oldmanService.getOldmanList(ids);
        return success(OldmanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得老人管理分页")
    public CommonResult<PageResult<OldmanRespVO>> getOldmanPage(@Valid OldmanPageReqVO pageVO) {
        PageResult<OldmanDO> pageResult = oldmanService.getOldmanPage(pageVO);
        return success(OldmanConvert.INSTANCE.convertPage(pageResult));
    }
}
